<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="warning" :disabled="selection.length == 0" @click="exportExcel">导出</el-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-date-picker v-model="search.date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" />
                    <el-input v-model="search.contract_code" placeholder="请输入销售单号" clearable @keyup.enter="upsearch"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column type="index" label="序号" fixed align="center" width="60"></el-table-column>
                <el-table-column label="销售单号" prop="contract_code" align="center"></el-table-column>
                <el-table-column label="销售日期" prop="open_single" align="center"></el-table-column>
                <el-table-column label="录单日期" prop="create_time" align="center"></el-table-column>
                <el-table-column label="销售人员" prop="seller.name" align="center"></el-table-column>
                <el-table-column label="操作" align="center" width="200">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'contract.show'">查看</el-button>
                            <el-button text type="primary" size="small" @click="table_mater(scope.row, scope.$index)">查看报料单</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <mater-list v-if="dialog.mater" ref="materDialog" @success="handleSuccess" @closed="dialog.mater = false"></mater-list>
</template>

<script>
import MaterList from './materList.vue'
import saveDialog from './save'

export default {
	name: 'BOM列表',
	components: {
		saveDialog,MaterList
	},
	data() {
        return {
			dialog: {
				save: false,
			},
			apiObj: this.$API.contract.contract.list,
			selection: [],
			search: {
				contract_code: null,
                date:null,
			},
            groupId: "1"
		}
	},
	mounted() {
	},
	methods: {
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
        table_mater(row) {
			this.dialog.mater = true
			this.$nextTick(() => {
				this.$refs.materDialog.open('add').setData(row)
			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        async exportExcel(){
            var ids = this.selection.map(v => v.id)
            let query = {
                export:2,
                id: ids
            }
            var res = await this.$API.contract.contract.listExport.get(query);
            if (res.code == 2) {
                const fileUrl = res.data.url;
                const link = document.createElement("a");
                link.href = fileUrl;
                link.download = fileUrl;
                link.click();
            } else {
                this.$alert(res.message, "提示", { type: "error" });
            }
        },
	}
}
</script>

<style></style>
